package pers.wk.queues.dlx;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import pers.wk.config.MQConfig;
import pers.wk.utils.MQConnectionUtils;

/**
 * 测试死信队列-生产者
 */
@Slf4j
public class Producer {

    public static void main(String[] args) {
        //1.ConnectionFactory
        ConnectionFactory connectionFactory = MQConnectionUtils.getConnectionFactory("/");
        Connection connection = null;
        Channel channel = null;
        try {
            //2.创建连接
            connection = connectionFactory.newConnection();
            //3.通过连接获取通道Channel
            channel = connection.createChannel();
            channel.exchangeDeclare(MQConfig.ExchangeName.DIRECT_EXCHANGE, MQConfig.ExchangeType.direct);
            System.out.println("测试死信队列-生产者启动。。。");

            String[] logLevel = {"email.error", "email.info"};
            for (int i = 0; i < 2; i++) {
                String routingKey = logLevel[i % 2];
                String message = "hello rabbitmq " + i;
                channel.basicPublish(MQConfig.ExchangeName.DIRECT_EXCHANGE, routingKey, null, message.getBytes());
                log.info("send message, routingKey: {}, message: {}", routingKey, message);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MQConnectionUtils.close(connection, channel);
        }
    }
}
